package org.foreningsgatan.genetics;

import java.io.Serializable;

public abstract class Node<T extends Serializable, S extends Serializable> implements Cloneable, Serializable {
	protected abstract void perform(SimulationEnvironment<T, S> environment);
	
	@SuppressWarnings("unchecked")
	protected Node<T, S> clone() {
		try {
			return (Node<T, S>) super.clone();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
	
	public void log(int indent) {
		String indentation = "";
		
		for (int i = 0; i < indent; i++) {
			indentation += "| ";
		}
		
		indentation += "'-";
		
		String desc = "";
		
		if (this instanceof ActionNode) {
			ActionNode<?, ?> a = (ActionNode<?, ?>) this;
			desc = "ActionNode : " + a.action.toString();
		} else if (this instanceof DecisionNode) {
			DecisionNode<?, ?> d = (DecisionNode<?, ?>) this;
			desc = "DecisionNode ? " + d.dataSource.toString() + " < " + d.limit;
		} else if (this instanceof MultipleNode) {
			desc = "MultipleNode";
		}
		
		System.out.println(indentation + desc);
		
		if (this instanceof MultipleNode) {
			MultipleNode<?, ?> m = (MultipleNode<?, ?>) this;
			m.leftNode.log(indent + 1);
			m.rightNode.log(indent + 1);
		}
	}
	
	protected int count() {
		return 1;
	}
}
